package net.dopan.pigframe.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.dopan.pigframe.core.base.controller.BaseController;
import net.dopan.pigframe.core.base.controller.response.ResponseBean;
import net.dopan.pigframe.core.bean.config.FilterIgnorePropertiesConfig;
import net.dopan.pigframe.core.utils.CommonConstant;
import net.dopan.pigframe.core.utils.IdUtils;
import net.dopan.pigframe.core.utils.SpringContextUtils;
import net.dopan.pigframe.demo.model.entity.UserDemo;
import net.dopan.pigframe.demo.service.UserDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Package: net.dopan.pigframe.demo.controller
 * @Title: DemoController
 * @Description:
 * @author: 刘宽
 * @date: 2018/10/15 15:02
 */
@RestController
@RequestMapping("/demo")
@Api(tags = "DemoController", description = "Demo例子")
public class DemoController extends BaseController {

    @Value("${demo.name}")
    private String demoName;

    @Qualifier("filterIgnorePropertiesConfig")
    @Autowired
    private FilterIgnorePropertiesConfig filterIgnorePropertiesConfig;

    @Autowired
    private UserDemoService userService;

    @ApiOperation(value = "hello", notes = "测试请求")
    @GetMapping("/hello")
    public ResponseBean<String> hello() throws Exception {
        System.out.println(demoName + IdUtils.nextId());
        System.out.println(filterIgnorePropertiesConfig.getUrls());
        Map<String, FilterIgnorePropertiesConfig.ServiceDetailBean> serviceDetails =
                filterIgnorePropertiesConfig.getServiceDetails();
        System.out.println(serviceDetails);
        for (Map.Entry<String, FilterIgnorePropertiesConfig.ServiceDetailBean> entry : serviceDetails.entrySet()) {
            System.out.println(entry.getKey());
            FilterIgnorePropertiesConfig.ServiceDetailBean entryValue = entry.getValue();
            Object bean = SpringContextUtils.getBean("net.dopan.pigframe.api.fegin." + entryValue.getServiceBean());
            Method method = bean.getClass().getMethod(entryValue.getServiceMethod(), String.class);
            Object admin = method.invoke(bean, "admin");
            System.out.println(admin);
        }
        return new ResponseBean<>(demoName + "  :" + IdUtils.nextId());
    }



    @DeleteMapping("test")
    public ResponseBean<UserDemo> test(@RequestBody UserDemo userDemo) throws Exception{
        /*Long gd = RedisUtils.incrBy("GD", 1);
        return new ResponseBean<>(""+gd);*/
        EntityWrapper<UserDemo> eWrapper = new EntityWrapper<UserDemo>(userDemo);
       userService.delete(eWrapper);
        return new ResponseBean<>(userDemo);
    }



    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "UserDemo")
    @PostMapping("/addUser")
    public ResponseBean<Boolean> addUser(@Valid UserDemo user) {
        return new ResponseBean<>(userService.addUser(user));
    }



    @ApiOperation(value = "删除用户", notes = "根据用户Id删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @DeleteMapping("/deleteUser/{id}")
    public ResponseBean<Boolean> deleteUser(@PathVariable Long id) {
        return new ResponseBean<>(userService.deleteById(id));
    }



    @ApiOperation(value = "查询用户", notes = "根据用户Id 查询用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @GetMapping("/findUser/{id}")
    public ResponseBean<UserDemo> findUser(@PathVariable Long id) {
        return new ResponseBean<>(userService.findById(id));
    }



    @ApiOperation(value = "更新用户", notes = "根据用户Id更新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "UserDemo")
    })
    @PutMapping("/updateUser/{id}")
    public ResponseBean<Boolean> updateUser(@PathVariable Long id, @RequestBody UserDemo user) {
        user.setId(id);
        System.out.println(user);
        return new ResponseBean<>(userService.myUpdateById(user));
    }



    @ApiOperation(value = "分页查询用户", notes = "分页查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageable", value = "分页实体类", dataType = "Pageable"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", dataType = "UserDemo")
    })
    @GetMapping("/getUser")
    public ResponseBean<Page<UserDemo>> getUser(UserDemo user, Pageable pageable) {
        user.setStatus(CommonConstant.STATUS_NORMAL);
        Page<UserDemo> page = new Page<UserDemo>(pageable.getPageNumber(), pageable.getPageSize());
        List<String> order = new ArrayList<>();
        order.add("id");
        EntityWrapper<UserDemo> eWrapper = new EntityWrapper<>();
        eWrapper.setEntity(user);
        // 降序
        eWrapper.orderDesc(order);
        return new ResponseBean<>(ResponseBean.SUCCESS, "success", userService.selectPage(page, eWrapper));
    }



    @ApiOperation(value = "查询所有用户", notes = "查询所有用户")
    @GetMapping("/getAll")
    public ResponseBean<List<UserDemo>> getAll() {
        return new ResponseBean<>(userService.getUser());
    }

}
